/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.jobmesmanage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.jobmesmanage.entity.JobWork;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.JobWorkService;
import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.JobLink;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.JobLinkService;

import java.util.List;

/**
 * 就业关联表Controller
 * @author zms
 * @version 2018-03-26
 */
@Controller
@RequestMapping(value = "${adminPath}/jobmesmanage/jobLink")
public class JobLinkController extends BaseController {

	@Autowired
	private JobLinkService jobLinkService;
	@Autowired
	private SystemService systemService;

	@Autowired
	private JobWorkService jobWorkService;

	@ModelAttribute
	public JobLink get(@RequestParam(required=false) String id) {
		JobLink entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = jobLinkService.get(id);
		}
		if (entity == null){
			entity = new JobLink();
		}
		return entity;
	}
	
	@RequiresPermissions("jobmesmanage:jobLink:view")
	@RequestMapping(value = {"list", ""})
	public String list(JobLink jobLink, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<JobLink> page = jobLinkService.findPage(new Page<JobLink>(request, response), jobLink);
		List<JobLink> jobLinks = jobLinkService.findList(jobLink);
		model.addAttribute("jobLinks", jobLinks);
		model.addAttribute("page", page);
		return "modules/jobmesmanage/jobLinkList";
	}

	@RequiresPermissions("jobmesmanage:jobLink:view")
	@RequestMapping(value = "form")
	public String form(JobLink jobLink, Model model) {
		JobWork jobWork = new JobWork();
		List<JobWork> jobWorks = jobWorkService.findList(jobWork);
		User campusinfoUser = new User();
		List<User> campusinfoUseres = systemService.findUser(campusinfoUser);
		model.addAttribute("campusinfoUseres", campusinfoUseres);
		model.addAttribute("jobLink", jobLink);
		model.addAttribute("jobWorks", jobWorks);
		return "modules/jobmesmanage/jobLinkForm";
	}

	@RequiresPermissions("jobmesmanage:jobLink:edit")
	@RequestMapping(value = "save")
	public String save(JobLink jobLink, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jobLink)){
			return form(jobLink, model);
		}
		jobLinkService.save(jobLink);
		addMessage(redirectAttributes, "保存就业报名成功");
		return "redirect:"+Global.getAdminPath()+"/jobmesmanage/jobLink/?repage";
	}
	
	@RequiresPermissions("jobmesmanage:jobLink:edit")
	@RequestMapping(value = "delete")
	public String delete(JobLink jobLink, RedirectAttributes redirectAttributes) {
		jobLinkService.delete(jobLink);
		addMessage(redirectAttributes, "删除就业报名成功");
		return "redirect:"+Global.getAdminPath()+"/jobmesmanage/jobLink/?repage";
	}

}